package com.diipo.weibo.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import android.util.Log;

public class TimeUtil {
	
	private static Date d2 ;
	
	private static long getSeconds(String time) {
        Date l_datetime = new Date();
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TimeZone l_timezone = TimeZone.getTimeZone("GMT+0");
        formatter.setTimeZone(l_timezone);
        
        String l_utc_date = formatter.format(l_datetime);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try
        {
            Date d1 = df.parse(l_utc_date);//当前时间
            d2 = df.parse(time);//获得时间
            
            long diff = d1.getTime() - d2.getTime();
            return diff;
        }
        catch (Exception e){
        	return 0;
        }
	}
	
	public static String convertToLocalTime(String utcDate){
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
        formatter2.setTimeZone(TimeZone.getDefault());
        
        try{
	        Date d1 = formatter.parse(utcDate);  
	        return formatter2.format(d1);
        }catch (Exception e){
        	return "";
        }
	}
	
//	public static void main(String[] args) {
//		
//		//System.out.println(getDate("2011-10-15 08:10:00"));
//		long l = getSeconds("2011-10-15 08:10:00");
//		System.out.println(getDate("2011-10-15 08:10:00"));
//		
//	}
	
	
	public static String getDate(String time){
		long reTime = getSeconds(time)/1000;
		if(reTime<0) reTime = 1;
		long d;
		Log.e(" pyb time 0", "  reTime =  " + reTime + "| <"+(4*7*24*60*60));
		if (reTime < 60) {
			return  reTime+" 秒前";
		}else if (reTime <(60*60)) {
			d= reTime/60;
			if (d<1) {
				d=1;
			}
			return  d+" 分钟前";
			
		}else if (reTime < (24*60*60)) {
			d= reTime/(60*60);
			if (d<1) {
				d=1;
			}
			return d +" 小时前";
			
		}else if (reTime <(7*24*60*60)) {
			d= reTime/(24*60*60);
			if (d<1) {
				d=1;
			}
			return d+" 天前";
			
		}else if (reTime < (4*7*24*60*60)) {
			Log.e(" pyb time 1", "  reTime =  " + reTime);
			d= reTime/(7*24*60*60);
			if (d<1) {
				d=1;
			}
			return d +" 周前";
			
		}else{
			d= reTime/(7*24*60*60);
			Log.e(" pyb time 2", "  reTime =  " + reTime + "| d="+d);
			return TimeUtil.convertToLocalTime(time);
		}
//		return time;
	}
}
